﻿#include "acceptfriend.h"
#include "ui_acceptfriend.h"
#include<QDebug>
AcceptFriend::AcceptFriend(string newFriendName,unordered_map<string,string>* newFriendPtr,bool* isopen,QPushButton* btn,SOCKET sockCli,string sender,QWidget *parent) :
    QWidget(parent),
    isopen(isopen),
    newFriendName(newFriendName),
    newFriendPtr(newFriendPtr),
    btn(btn),
    sockCli(sockCli),
    sender(sender),
    ui(new Ui::AcceptFriend)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose, true);
    *isopen = true;
    this->setWindowTitle(QString::fromStdString(newFriendName));
    string remarks = newFriendPtr->find(newFriendName)->second;
    ui->textBrowser->setText(QString::fromStdString(remarks));
}

AcceptFriend::~AcceptFriend()
{
    qDebug()<<u8"AcceptFriend析构!";
    *isopen = false;
    delete ui;
}
//同意
void AcceptFriend::on_pushButton_clicked()
{
    //隐藏按钮
    btn->setVisible(false);
    //发送信息
    m.messageType = 6;
    strcpy(m.sender,sender.c_str());
    strcpy(m.recevicer,newFriendName.c_str());
    strcpy(m.messageContent,"T");
    send(sockCli,(char*)&m,sizeof(m),0);
    //清除该信息
    newFriendPtr->erase(newFriendPtr->find(newFriendName.substr(0,4)));
    //关闭
    this->close();
}
//拒绝
void AcceptFriend::on_pushButton_2_clicked()
{
    //隐藏按钮
    btn->setVisible(false);
    //发送信息
    m.messageType = 6;
    strcpy(m.sender,sender.c_str());
    strcpy(m.recevicer,newFriendName.c_str());
    strcpy(m.messageContent,"F");
    send(sockCli,(char*)&m,sizeof(m),0);
    //清除该信息
    newFriendPtr->erase(newFriendPtr->find(newFriendName.substr(0,4)));
    //关闭
    this->close();
}
